<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.css">

    <script src="js/jquery.min.js"></script>
</head>

<body style="padding: 15px;">


    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">图书管理</h3>
        </div>
        <div class="panel-body form-inline">

            <div class="input-group">
                <div class="input-group-addon">书名</div>
                <input type="text" class="form-control bookname" id="exampleInputAmount" placeholder="请输入书名">
            </div>
            <div class="input-group">
                <div class="input-group-addon">作者</div>
                <input type="text" class="form-control author" id="exampleInputAmount" placeholder="请输入作者">
            </div>
            <div class="input-group">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control publisher" id="exampleInputAmount" placeholder="请输入出版社">
            </div>
            <button class="btn btn-primary" id='btnadd'>添加</button>
        </div>
    </div>

    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody class="tb">
            <tr>
                <td></td>
            </tr>
        </tbody>
    </table>
    <script>
        $(function () {
            function getbook() {
                $.get('http://www.liulongbin.top:3006/api/getbooks', function (e) {
                    if (e.status !== 200) return alert('请求失败！')
                    var a = '';
                    $.each(e.data, function (i, e) {
                        a += `<tr><td>${e.id}</td><td>${e.bookname}</td><td>${e.author}</td><td>${e.publisher}</td><td><a class='a' href='javascript:;' data-index='${e.id}'>删除</a></td></tr>`
                    })
                    $('.tb').empty().html(a)
                })
            }
            getbook();
            $('.tb').on('click', '.a', function () {
                var id = $(this).attr('data-index');
                $.get('http://www.liulongbin.top:3006/api/delbook', { id: id }, function (e) {
                    if (e.status !== 200) return alert('删除失败')
                    getbook();
                })

            })
            $('#btnadd').on('click', function () {
                var bookname = $('.bookname').val();
                var reg = /[\u4e00-\u9fa5]/;
                var author = $('.author').val();
                var publisher = $('.publisher').val();
                var result3 = reg.test(publisher);
                var result2 = reg.test(author);
                var result1 = reg.test(bookname);
                if (bookname.length <= 0 || author.length <= 0 || publisher.length <= 0 || !result1 || !result2 || !result3) { return alert('请重新输入') }
                $.post('http://www.liulongbin.top:3006/api/addbook', { bookname: bookname, author: author, publisher: publisher }, function (e) {
                    if (e.status !== 201) return alert('添加失败')
                    getbook();
                    $('.bookname').val('');
                    $('.author').val('');
                    $('.publisher').val('');
                })
            })

        })
    </script>

</body>

</html>